<template>
  <div class="app-container">
    <div class="body">
      <el-row>
        <el-col :span="24">
          <!-- 查询表单 -->
          <el-row>
            <el-form
              :inline="true"
              size="mini"
              :model="searchForm"
              ref="searchForm"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input
                  v-model="searchForm.keyword"
                  placeholder="请输入标题/子标题"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item label="视频状态">
                <el-select v-model="searchForm.status" clearable>
                  <el-option
                    v-for="item in statusList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="是否删除">
                <el-select v-model="searchForm.deleted">
                  <el-option
                    v-for="item in deletedList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <br />
              <el-form-item label="创建/发布时间">
                <el-col :span="11">
                  <el-date-picker
                    type="date"
                    placeholder="开始日期"
                    v-model="searchForm.beginDate"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    style="width: 100%"
                  ></el-date-picker>
                </el-col>
                <el-col class="line" :span="2" style="align-content: center"
                  >-</el-col
                >
                <el-col :span="11">
                  <el-date-picker
                    type="date"
                    placeholder="结束时间"
                    v-model="searchForm.endDate"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    style="width: 100%"
                  ></el-date-picker>
                </el-col>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="handleQuery"
                  >查询</el-button
                >
              </el-form-item>
              <el-form-item>
                <el-button
                  type="success"
                  plain
                  icon="el-icon-plus"
                  @click="handleSave()"
                  >新增</el-button
                >
              </el-form-item>
            </el-form>
          </el-row>
          <!-- 表格 -->
          <el-row>
            <el-table stripe :data="tableData">
              <el-table-column
                prop="id"
                label="编号"
                width="80"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                prop="title"
                label="标题"
                width="180"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                prop="subTitle"
                label="子标题"
                width="180"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                prop="description"
                label="描述"
                width="180"
                :show-overflow-tooltip="true"
              />
              <el-table-column label="封面" prop="coverUrl">
                <template v-slot="scope">
                  <el-avatar
                    :size="50"
                    :src="scope.row.coverUrl || defaultHeadImg"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="videoUrl"
                label="视频地址"
                width="180"
                :show-overflow-tooltip="true"
              />
              <el-table-column prop="authorId" label="作者" />
              <el-table-column prop="likes" label="点赞数" width="100">
                <template v-slot="scope">
                  <el-badge :value="scope.row.likes" class="badge"
                    >点赞数
                  </el-badge>
                </template>
              </el-table-column>
              <el-table-column prop="coins" label="投币数" width="100">
                <template v-slot="scope">
                  <el-badge :value="scope.row.coins" class="badge"
                    >投币数
                  </el-badge>
                </template>
              </el-table-column>
              <el-table-column prop="shares" label="分享数" width="100">
                <template v-slot="scope">
                  <el-badge :value="scope.row.shares" class="badge"
                    >分享数
                  </el-badge>
                </template>
              </el-table-column>
              <el-table-column prop="comments" label="评论数" width="100">
                <template v-slot="scope">
                  <el-badge :value="scope.row.comments" class="badge"
                    >评论数
                  </el-badge>
                </template>
              </el-table-column>
              <el-table-column prop="views" label="观看数" width="100">
                <template v-slot="scope">
                  <el-badge :value="scope.row.views" class="badge"
                    >观看数
                  </el-badge>
                </template>
              </el-table-column>
              <el-table-column prop="status" label="视频状态">
                <template v-slot="scope">
                  {{
                    scope.row.status === 0
                      ? "未发布"
                      : scope.row.status === 1
                      ? "已发布"
                      : "已禁用"
                  }}
                </template>
              </el-table-column>
              <el-table-column
                prop="releaseTime"
                label="发布时间"
                width="180"
              />
              <el-table-column
                prop="createdTime"
                label="创建时间"
                width="180"
              />
              <el-table-column
                prop="updatedTime"
                label="更新时间"
                width="180"
              />
              <el-table-column label="操作" width="200" fixed="right">
                <template v-slot="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    style="margin-right: 10px"
                    @click="handleEdit(scope.row)"
                    >编辑
                  </el-button>
                  <el-popconfirm
                    confirm-button-text="确认"
                    cancel-button-text="取消"
                    @confirm="handleDelete(scope.row.id, !scope.row.deleted)"
                    @onConfirm="handleDelete(scope.row.id, !scope.row.deleted)"
                    icon="el-icon-info"
                    icon-color="red"
                    :title="`确定${
                      scope.row.deleted ? '恢复' : '删除'
                    }这个视频吗？`"
                  >
                    <el-button
                      size="mini"
                      :type="scope.row.deleted ? 'success' : 'danger'"
                      :icon="
                        scope.row.deleted ? 'el-icon-rank' : 'el-icon-delete'
                      "
                      slot="reference"
                      >{{ scope.row.deleted ? "恢复" : "删除" }}
                    </el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
          <!-- 分页 -->
          <el-row>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="searchForm.currentPage"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="searchForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        status-icon
        ref="editForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-input type="hidden" v-show="false" v-model="editForm.id" />
        <el-form-item label="标题" prop="title">
          <el-input
            type="textarea"
            v-model="editForm.title"
            :rows="2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="子标题" prop="subTitle">
          <el-input
            type="textarea"
            v-model="editForm.subTitle"
            :rows="2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            v-model="editForm.description"
            :rows="2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="视频状态" prop="status">
          <el-radio-group v-model="editForm.status">
            <el-radio :label="0">未发布</el-radio>
            <el-radio :label="1">已发布</el-radio>
            <el-radio :label="2">已禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoUrl">
          <el-input v-model="editForm.videoUrl" />
        </el-form-item>
        <el-form-item label="作者" prop="authorId">
          <el-input v-model="editForm.authorId" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  changeUserStatus,
  deleteById,
  listData,
  saveOrUpdate,
} from "@/api/business/video";

export default {
  name: "Video",
  data() {
    return {
      defaultHeadImg:
        "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png",
      dialogTitle: "",
      dialogFormVisible: false,
      editForm: {
        dept: {},
        title: "",
        status: "0",
      },
      statusList: [
        { value: 0, label: "未发布" },
        { value: 1, label: "已发布" },
        { value: 2, label: "已禁用" },
      ],
      deletedList: [
        { value: 0, label: "正常" },
        { value: 1, label: "已删除" },
      ],
      rules: {
        title: [{ required: true, message: "标题不能为空" }],
      },
      total: 0,
      tableData: [],
      loading: true,
      searchForm: {
        keyword: "",
        status: undefined,
        deleted: 0,
        beginDate: null,
        endDate: null,
        currentPage: 1,
        pageSize: 3,
      },
    };
  },
  methods: {
    reset() {
      this.editForm = {
        dept: {},
        deleted: 0,
        status: 0,
      };
      this.resetForm("editForm");
    },
    fetchData(params) {
      this.loading = true;
      listData(params).then((res) => {
        const { data } = res;
        this.searchForm.currentPage = data.currentPage;
        this.searchForm.pageSize = data.pageSize;
        this.total = data.totalCount;
        this.tableData = data.data;
        this.loading = false;
      });
    },
    handleCurrentChange(current) {
      this.searchForm.currentPage = current;
      // 当每页显示数发生变动时触发
      this.fetchData(this.searchForm);
    },
    handleSizeChange(size) {
      this.searchForm.pageSize = size;
      // 当前页发生变动时触发
      this.fetchData(this.searchForm);
    },
    handleQuery() {
      // 提交查询表单
      this.searchForm.currentPage = 1;
      this.fetchData(this.searchForm);
    },
    submitForm() {
      // 增加表单校验拦截, 如果表单校验不通过, 不应该关闭弹窗
      this.$refs.editForm.validate().then((valid) => {
        // 获取到当前的表单数据, 并发送请求到后台
        saveOrUpdate(this.editForm).then((res) => {
          // 保存成功后重新刷新表格
          this.fetchData();
          // 提示用户保存成功
          this.$message.success("操作成功!");
          // 隐藏弹框
          this.dialogFormVisible = false;
        });
      });
    },
    handleSave() {
      // 1. 修改标题为 新增用户
      this.dialogTitle = "新增视频";
      // 2. 将表达的数据清空
      this.reset();
      // 3. 显示对话框
      this.dialogFormVisible = true;
    },
    handleEdit(data) {
      // 点击编辑按钮触发
      // 1. 修改标题为 编辑用户
      this.dialogTitle = "编辑视频";
      // 2. 将表单的数据清空
      this.editForm = data;
      // 3. 显示对话框
      this.dialogFormVisible = true;
    },
    handleDelete(id, deleted) {
      // 点击删除按钮触发
      // 1. 先弹出确认框, 确认是否删除
      // 2. 点击确认按钮时, 才发送请求删除数据
      deleteById({ id, deleted }).then((data) => {
        // 3. 如果删除成功, 提示删除成功
        this.$message.success("操作成功");
        // 4. 刷新页面的数据
        this.searchForm.deleted = 0;
        this.fetchData(this.searchForm);
      });
    },
  },
  created() {
    // 获取用户数据
    this.fetchData(this.searchForm);
  },
};
</script>

<!--
  三部分: 写样式, 在这里面可以写普通 css 样式, 也可以写 less/sass/scss 等样式, 但是需要 css 预处理插件处理
    1. scoped: 表示这里面写的样式只会影响到这个 vue 文件中的模板, 不会影响到其他模板, 如果不写的话就属于全局样式, 会对其他模块造成影响
    2. lang: 可以描述里面的内容是 css/less/sass/scss
-->
<style>
.line {
  text-align: center;
}

.el-pagination {
  text-align: right;
}

.el-badge {
  line-height: 30px;
}

.el-badge__content.is-fixed {
  position: absolute !important;
  top: 10px !important;
  right: 10px !important;
}
</style>
